package leetcode;

import org.springframework.data.redis.hash.HashMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhoub on 2021/2/22.
 */
public class T0001 {
    public int[] twoSum(int[] nums, int target) {

        //第一种：两重for循环，时间复杂度为n^2
        /*for(int i=0;i<nums.length;i++){
            for(int j=i+1;j<nums.length;j++){
                if(nums[i]+nums[j]==target){
                    return new int[]{i,j};
                }
            }
        }*/


        Map<Integer,Integer> ref = new HashMap<>(nums.length,1);
        for (int i = 0; i < nums.length; i++) {
            if(ref.containsKey(nums[i])){
                return new int[]{ref.get(nums[i]),i};
            }else{
                ref.put(target-nums[i],i);
            }
        }
        return null;
    }
}
